package com.weiyan.filter;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * @author misterWei
 * @create 2020年02月27号:16点09分
 * @mailbox mynameisweiyan@gmail.com
 *
 * 这是一个自定义的内置局部过滤器处理器
 * */
@Component
@Slf4j
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {
   public LogGatewayFilterFactory(){
       super(LogGatewayFilterFactory.Config.class);
   }



    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("isOpen");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                if (config.getIsOpen()){
                    log.info("日志状态为:{}",config.getIsOpen());
                }
                //过滤器链继续执行
                return chain.filter(exchange);
            }
        };
    }



    /**
     * 必须是静态类
     */

    @NoArgsConstructor
    public static class Config {
       private boolean isOpen;

        public boolean getIsOpen() {
            return isOpen;
        }

        public void setIsOpen(boolean isOpen) {
            this.isOpen = isOpen;
        }
    }
}
